﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GrammarCheckerPlugin
{
    public partial class SpellingForm : Form
    {
        public string SelectedWord { get; set; }

        public SpellingForm()
        {
            InitializeComponent();
        }

        public void InitSpellingForm(string description, string currentText, string notInDictionary, string[] suggestions, string helpText)
        {
            lblHelpTitle.Text = description;
            webCurrentText.DocumentText = currentText;
            txtNotInDictionary.Text = notInDictionary;

            foreach (var suggestion in suggestions)
            {
                lstSuggestions.Items.Add(suggestion);
            }

            if (!string.IsNullOrEmpty(helpText))
            {
                webHelp.DocumentText = helpText;
            }

            linkDonate.Links.Add(new LinkLabel.Link(0, linkDonate.Text.Length, "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=R79MDALNG9NKW"));
            linkDonate.LinkClicked += new LinkLabelLinkClickedEventHandler(linkDonate_LinkClicked);
        }

        private void linkDonate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Determine which link was clicked within the LinkLabel.
            linkDonate.Links[linkDonate.Links.IndexOf(e.Link)].Visited = true;
            
            // Display the appropriate link based on the value of the 
            // LinkData property of the Link object.
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            SelectedWord = txtNotInDictionary.Text;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnIgnore_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Ignore;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void lstSuggestions_SelectedValueChanged(object sender, EventArgs e)
        {
            if (lstSuggestions.SelectedItem != null)
            {
                txtNotInDictionary.Text = lstSuggestions.SelectedItem as string;
            }
        }

        private void lstSuggestions_DoubleClick(object sender, EventArgs e)
        {
            lstSuggestions_SelectedValueChanged(null, null);
            btnChange_Click(null, null);
        }
    }
}
